package com.prs.crm.action.purchase;

import java.io.File;
import java.util.Collection;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

import com.prs.crm.action.FlowAction;
import com.prs.crm.annotation.Clew;
import com.prs.crm.annotation.FlowActive;
import com.prs.crm.annotation.FlowStart;
import com.prs.crm.context.Constrains;
import com.prs.crm.domain.fs.BillType;
import com.prs.crm.domain.fs.PayWay;
import com.prs.crm.domain.purchase.Inquiry;
import com.prs.crm.domain.purchase.PaymentDetails;
import com.prs.crm.domain.purchase.PaymentGather;
import com.prs.crm.domain.purchase.PaymentOrder;
import com.prs.crm.domain.purchase.PurchaseOrder;
import com.prs.crm.domain.purchase.PurchaseOrderDetail;
import com.prs.crm.domain.purchase.SupplierInquiry;
import com.prs.crm.domain.stock.Product;
import com.prs.crm.domain.stock.PurchaseType;
import com.prs.crm.domain.stock.Supplier;
import com.prs.crm.domain.store.StoreRoom;
import com.prs.crm.domain.sys.Code;
import com.prs.crm.domain.sys.CodeInvoice;
import com.prs.crm.service.fs.BillTypeService;
import com.prs.crm.service.fs.PayWayService;
import com.prs.crm.service.purchase.PaymentGatherService;
import com.prs.crm.service.purchase.PurchaseOrderService;
import com.prs.crm.service.purchase.SupplierInquiryService;
import com.prs.crm.service.stock.PurchaseTypeService;
import com.prs.crm.service.stock.StoreRoomService;
import com.prs.crm.service.stock.SupplierService;
import com.prs.crm.service.sys.CodeInvoiceService;
import com.prs.crm.service.sys.CodeService;

/**
 * @author 兰小刚
 * @version 创建时间：2010-6-30 下午02:45:10 类说明
 */

public class PurchaseOrderAction extends FlowAction<PurchaseOrder> {

	private static final long serialVersionUID = 1L;

	private String Encode;
	private CodeInvoice codeInvoice;
	private CodeInvoiceService codeInvoiceService;
	private Code code;
	private CodeService codeService;
	private Collection<CodeInvoice> codelist;
	private PaymentOrder paymentOrder;

	private StoreRoom room;

	private PayWay payWay;

	private BillType billtype;

	private PaymentGather paymentGather;

	private PurchaseOrder purchaseOrder;

	private SupplierInquiry supplierInquiry;

	private Inquiry inquiry;

	private Supplier supplier;

	@Autowired
	private BillTypeService billtypeService;

	private PayWayService payWayService;

	private PaymentGatherService paymentGatherService;

	private StoreRoomService storeRoomService;

	private PurchaseOrderService purchaseOrderService;

	private SupplierService supplierService;

	private PaginationSupport pagination;

	private List<PurchaseType> purchaseTypeList;

	private List<Inquiry> inquirys;

	private List<PayWay> payWaylist;

	private List<BillType> billTypelist;

	private List<StoreRoom> rooms;

	private Collection<PurchaseOrderDetail> slist;

	private Collection<PaymentDetails> gatherlist;

	private PurchaseTypeService purchaseTypeService;

	private SupplierInquiryService supplierInquiryService;

	private Product[] products;

	private String[] productNames;

	private Double[] markprices;

	private Integer[] stockNums;

	private Double[] prices;

	private Double[] discounts;

	private Double[] totolMonlys;
	private String listString;
	private String state = "c";
	private String flow;
	private File upload;
	private String uploadS;
	private String uploadFileName;

	@FlowActive(target = "purchaseOrder", flowservice = "purchaseOrderService")
	@Action("editPurchaseToOrder")
	public String editPurchaseToOrder() {
		this.purchaseTypeList = this.getPurchaseTypeService().getAllTypes();
		return SUCCESS;
	}

	@FlowActive(target = "purchaseOrder", flowservice = "purchaseOrderService")
	@Action("editNewPurchaseOrder")
	public String editNewPurchaseOrder() {
		this.purchaseTypeList = this.getPurchaseTypeService().getAllTypes();
		this.codelist = this.getCodeInvoiceService().getInvoiceCodeType("采购订单");
		return SUCCESS;
	}

	@Clew(results = { "success", "none" })
	@FlowStart(flowservice = "purchaseOrderService", target = "purchaseOrder")
	@Action(value = "savePurchaseOrder", results = {
			@Result(name = "success", type = "redirect", location = "listPurchaseOrder"),
			@Result(name = "error", type = "chain", location = "editPurchaseToOrder"),
			@Result(name = "none", type = "redirect", location = "listPurchaseOrder") })
	public String savePurchaseOrder() {
		state = "a";
		if (flow == null || flow.equals("")) {
			this.addActionError("审核流程不能为空");
			return ERROR;
		}
		if (purchaseOrder.getPurchaseTile() == null
				|| purchaseOrder.getPurchaseTile().equals("")) {
			this.addActionError("采购主题不能为空");
			return ERROR;
		}
		if (purchaseOrder.getPurchaseCode() == null
				|| purchaseOrder.getPurchaseCode().equals("")) {
			this.addActionError("采购编号不能为空");
			return ERROR;
		}
		if (purchaseOrder.getSupplier() == null) {
			this.addActionError("供应商不能为空");
			return ERROR;
		}
		if (purchaseOrder.getPurchaseType() == null) {
			this.addActionError("采购类型不能为空");
			return ERROR;
		}
		if (purchaseOrder.getPrincipal() == null) {
			this.addActionError("我方代表不能为空");
			return ERROR;
		}
		if (purchaseOrder.getOppositePerson() == null
				|| purchaseOrder.getOppositePerson().equals("")) {
			this.addActionError("对方代表不能为空");
			return ERROR;
		}
		if (products == null) {
			this.addActionError("采购明细不能为空");
			return ERROR;
		}
		if (purchaseOrder.getPurchaseDate() == null) {
			this.addActionError("采购日期不能为空");
			return ERROR;
		}
		this.getPurchaseOrderService().savePurchaseOrderDetail(purchaseOrder,
				products, productNames, markprices, stockNums, markprices,
				discounts, totolMonlys, upload, uploadFileName);
		SupplierInquiry SupplierInquiryObject = this
				.getSupplierInquiryService().getSupplierInquiry(
						supplierInquiry.getId());
		SupplierInquiryObject.setOrderTag(2);
		this.getSupplierInquiryService().saveOrUapedate(SupplierInquiryObject);

		this.setPrompt("转订单成功");
		return SUCCESS;
	}

	@Clew(results = { "success", "none" })
	@FlowStart(flowservice = "purchaseOrderService", target = "purchaseOrder")
	@Action(value = "saveNewPurchaseOrder", results = {
			@Result(name = "success", type = "redirect", location = "listPurchaseOrder"),
			@Result(name = "error", type = "chain", location = "editNewPurchaseOrder"),
			@Result(name = "none", type = "redirect", location = "listPurchaseOrder") })
	public String saveNewPurchaseOrder() {
		upload=new File(uploadS);
		if ("n".equals(Encode)) {
			int EncodeRplice = this.getCodeService().getBackRpliceAll("手动编码",
					"采购订单", purchaseOrder.getPurchaseCode());
			if (EncodeRplice != 0) {
				this.addActionError("编码重复!请重新输入");
				return ERROR;
			}
		}
		if ("x".equals(Encode) || "".equals(Encode)) {
			this.addActionError("请选择编码类型");
			return ERROR;
		}
		if (flow == null || flow.equals("")) {
			this.addActionError("审核流程不能为空");
			return ERROR;
		}
		if (purchaseOrder.getPurchaseTile() == null
				|| purchaseOrder.getPurchaseTile().equals("")) {
			this.addActionError("采购主题不能为空");
			return ERROR;
		}
		if (purchaseOrder.getPurchaseCode() == null
				|| purchaseOrder.getPurchaseCode().equals("")) {
			this.addActionError("采购编号不能为空");
			return ERROR;
		}
		if (purchaseOrder.getPurchaseDate() == null) {
			this.addActionError("采购日期不能为空");
			return ERROR;
		}
		if (purchaseOrder.getPurchaseType() == null) {
			this.addActionError("采购分类不能为空");
			return ERROR;
		}
		if (purchaseOrder.getPrincipal() == null) {
			this.addActionError("我方代表不能为空");
			return ERROR;
		}
		if (purchaseOrder.getOppositePerson() == null
				|| purchaseOrder.getOppositePerson().equals("")) {
			this.addActionError("对方代表不能为空");
			return ERROR;
		}
		if (products == null) {
			this.addActionError("采购明细不能为空");
			return ERROR;
		}

		/**
		 * 编码产生
		 */
		if ("n".equals(Encode)) {
			Code codes = new Code();
			codes.setCodeValue(purchaseOrder.getPurchaseCode());
			codes.setCodeMolde("采购订单");
			codes.setEntityName(this.getClass().getName());
			codes.setCodeType("手动编码");
			this.getCodeService().save(codes);
			purchaseOrder.setCode(codes);
			this.getPurchaseOrderService().savePurchaseOrderDetail(
					purchaseOrder, products, productNames, markprices,
					stockNums, markprices, discounts, totolMonlys, upload,
					uploadFileName);
		} else {
			CodeInvoice Vob = this.getCodeInvoiceService().getVobject(
					codeInvoice.getId());
			Code codes = new Code();
			codes.setCodeValue(purchaseOrder.getPurchaseCode());
			codes.setCodeMolde("采购订单");
			codes.setEntityName(this.getClass().getName());
			Vob.setNumBackUp(Increase(Vob.getNumBackUp()));
			this.getCodeInvoiceService().saveOrUpdate(Vob);
			codes.setCodeType(Vob.getCodePlan());
			this.getCodeService().save(codes);
			purchaseOrder.setCode(codes);
			
			this.getPurchaseOrderService().savePurchaseOrderDetail(
					purchaseOrder, products, productNames, markprices,
					stockNums, markprices, discounts, totolMonlys, upload,
					uploadFileName);
		}

		this.setPrompt("新产生订单成功");
		return SUCCESS;
	}

	@Action("listPurchaseOrder")
	public String excute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				PurchaseOrder.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getPurchaseOrderService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("listPurchaseOrderProcess")
	public String excute1() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				PurchaseOrder.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		criteria.add(Property.forName("flowstatus").eq(
				Constrains.PURCHASEORDERPASS));
		criteria.add(Restrictions.or(Restrictions.eq("purStoreState", 0),
				Restrictions.eq("purPayState", 0)));
		// criteria.add(Property.forName("processTag").eq(
		// 0));
		criteria.addOrder(Order.desc("purchaseDate"));
		this.setPagination(this.getPurchaseOrderService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("listCompeleteOrder")
	public String excute2() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				PurchaseOrder.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		criteria.add(Property.forName("flowstatus").eq(
				Constrains.PURCHASEORDERPASS));
		this.setPagination(this.getPurchaseOrderService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("listReturnedPurchase")
	public String excute3() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				PurchaseOrder.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		criteria.add(Property.forName("flowstatus").eq(
				Constrains.PURCHASEORDERPASS));
		criteria.add(Property.forName("purchaseState").eq(
				Constrains.COMPELETEPURCHASE));
		this.setPagination(this.getPurchaseOrderService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("selectSupplier")
	public String selectSupplier() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Supplier.class);
		this.setPagination(this.getSupplierService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}

	@Action("selectPurchaseOrderProduct")
	public String selectPurchaseOrderProduct() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Product.class);
		this.setPagination(this.getSupplierService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}

	@Action("viewPurchaseOrder")
	public String viewPurchaseOrder() {
		purchaseOrder = this.getPurchaseOrderService().get(
				purchaseOrder.getId());
		this.slist = purchaseOrder.getDetails();
		this.displayEndNodes(purchaseOrder);
		System.out.println(purchaseOrder.getPurchaseOrderAttach());
		return SUCCESS;
	}

	@Action("editPurchaseOrderToPayment")
	public String editPurchaseOrderToPayment() {
		purchaseOrder = this.getPurchaseOrderService().get(
				purchaseOrder.getId());
		this.slist = purchaseOrder.getDetails();
		this.displayEndNodes(purchaseOrder);
		return SUCCESS;
	}

	@Action("editPurchaseOrderToStoreIn")
	public String editPurchaseOrderToStoreIn() {

		this.rooms = this.getStoreRoomService().getAllstoreroom();
		this.codelist = this.getCodeInvoiceService().getInvoiceCodeType("入库单");
		purchaseOrder = this.getPurchaseOrderService().get(
				purchaseOrder.getId());
		this.slist = purchaseOrder.getDetails();
		this.displayEndNodes(purchaseOrder);
		return SUCCESS;
	}

	@Action("editPayPlan")
	public String editPayPlan() {
		return SUCCESS;
	}

	@Action("editNotNullPayPlan")
	public String editNotNullPayPlan() {

		paymentGather = this.getPaymentGatherService().get(
				paymentOrder.getPaymentGather().getId());
		this.gatherlist = paymentGather.getDetails();
		return SUCCESS;
	}

	@Action("editAddPaymentPlan")
	public String editAddPaymentPlan() {
		this.codelist = this.getCodeInvoiceService()
				.getInvoiceCodeType("采购票据单");
		payWaylist = this.getPayWayService().getAll();
		billTypelist = this.getBilltypeService().getAll();
		return SUCCESS;
	}

	public static String Increase(String backupNum) {
		String Inc = "";
		int lengths = backupNum.length();
		String lengthss = (Integer.parseInt(backupNum) + 1) + "";
		int increase = (lengthss.trim()).length();
		if (lengths != increase) {
			String In = "";
			for (int i = 0; i < lengths - increase; i++) {
				In = In + "0";
			}
			In = (In + lengthss).trim();
			Inc = In;
		} else {
			Inc = lengthss;
		}
		return Inc.trim();
	}

	public PurchaseOrder getPurchaseOrder() {
		return purchaseOrder;
	}

	public void setPurchaseOrder(PurchaseOrder purchaseOrder) {
		this.purchaseOrder = purchaseOrder;
	}

	public PurchaseOrderService getPurchaseOrderService() {
		return purchaseOrderService;
	}

	public void setPurchaseOrderService(
			PurchaseOrderService purchaseOrderService) {
		this.purchaseOrderService = purchaseOrderService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public List<PurchaseType> getPurchaseTypeList() {
		return purchaseTypeList;
	}

	public void setPurchaseTypeList(List<PurchaseType> purchaseTypeList) {
		this.purchaseTypeList = purchaseTypeList;
	}

	public PurchaseTypeService getPurchaseTypeService() {
		return purchaseTypeService;
	}

	public void setPurchaseTypeService(PurchaseTypeService purchaseTypeService) {
		this.purchaseTypeService = purchaseTypeService;
	}

	public SupplierInquiry getSupplierInquiry() {
		return supplierInquiry;
	}

	public void setSupplierInquiry(SupplierInquiry supplierInquiry) {
		this.supplierInquiry = supplierInquiry;
	}

	public Inquiry getInquiry() {
		return inquiry;
	}

	public void setInquiry(Inquiry inquiry) {
		this.inquiry = inquiry;
	}

	public List<Inquiry> getInquirys() {
		return inquirys;
	}

	public void setInquirys(List<Inquiry> inquirys) {
		this.inquirys = inquirys;
	}

	public SupplierInquiryService getSupplierInquiryService() {
		return supplierInquiryService;
	}

	public void setSupplierInquiryService(
			SupplierInquiryService supplierInquiryService) {
		this.supplierInquiryService = supplierInquiryService;
	}

	public Product[] getProducts() {
		return products;
	}

	public void setProducts(Product[] products) {
		this.products = products;
	}

	public String[] getProductNames() {
		return productNames;
	}

	public void setProductNames(String[] productNames) {
		this.productNames = productNames;
	}

	public Double[] getMarkprices() {
		return markprices;
	}

	public void setMarkprices(Double[] markprices) {
		this.markprices = markprices;
	}

	public Integer[] getStockNums() {
		return stockNums;
	}

	public void setStockNums(Integer[] stockNums) {
		this.stockNums = stockNums;
	}

	public Double[] getPrices() {
		return prices;
	}

	public void setPrices(Double[] prices) {
		this.prices = prices;
	}

	public Double[] getDiscounts() {
		return discounts;
	}

	public void setDiscounts(Double[] discounts) {
		this.discounts = discounts;
	}

	public Double[] getTotolMonlys() {
		return totolMonlys;
	}

	public void setTotolMonlys(Double[] totolMonlys) {
		this.totolMonlys = totolMonlys;
	}

	public Supplier getSupplier() {
		return supplier;
	}

	public void setSupplier(Supplier supplier) {
		this.supplier = supplier;
	}

	public SupplierService getSupplierService() {
		return supplierService;
	}

	public void setSupplierService(SupplierService supplierService) {
		this.supplierService = supplierService;
	}

	public Collection<PurchaseOrderDetail> getSlist() {
		return slist;
	}

	public void setSlist(Collection<PurchaseOrderDetail> slist) {
		this.slist = slist;
	}

	public PayWay getPayWay() {
		return payWay;
	}

	public void setPayWay(PayWay payWay) {
		this.payWay = payWay;
	}

	public BillType getBilltype() {
		return billtype;
	}

	public void setBilltype(BillType billtype) {
		this.billtype = billtype;
	}

	public BillTypeService getBilltypeService() {
		return billtypeService;
	}

	public void setBilltypeService(BillTypeService billtypeService) {
		this.billtypeService = billtypeService;
	}

	public PayWayService getPayWayService() {
		return payWayService;
	}

	public void setPayWayService(PayWayService payWayService) {
		this.payWayService = payWayService;
	}

	public List<PayWay> getPayWaylist() {
		return payWaylist;
	}

	public void setPayWaylist(List<PayWay> payWaylist) {
		this.payWaylist = payWaylist;
	}

	public List<BillType> getBillTypelist() {
		return billTypelist;
	}

	public void setBillTypelist(List<BillType> billTypelist) {
		this.billTypelist = billTypelist;
	}

	public PaymentOrder getPaymentOrder() {
		return paymentOrder;
	}

	public void setPaymentOrder(PaymentOrder paymentOrder) {
		this.paymentOrder = paymentOrder;
	}

	public PaymentGather getPaymentGather() {
		return paymentGather;
	}

	public void setPaymentGather(PaymentGather paymentGather) {
		this.paymentGather = paymentGather;
	}

	public PaymentGatherService getPaymentGatherService() {
		return paymentGatherService;
	}

	public void setPaymentGatherService(
			PaymentGatherService paymentGatherService) {
		this.paymentGatherService = paymentGatherService;
	}

	public Collection<PaymentDetails> getGatherlist() {
		return gatherlist;
	}

	public void setGatherlist(Collection<PaymentDetails> gatherlist) {
		this.gatherlist = gatherlist;
	}

	public StoreRoom getRoom() {
		return room;
	}

	public void setRoom(StoreRoom room) {
		this.room = room;
	}

	public StoreRoomService getStoreRoomService() {
		return storeRoomService;
	}

	public void setStoreRoomService(StoreRoomService storeRoomService) {
		this.storeRoomService = storeRoomService;
	}

	public List<StoreRoom> getRooms() {
		return rooms;
	}

	public void setRooms(List<StoreRoom> rooms) {
		this.rooms = rooms;
	}

	public String getListString() {
		return listString;
	}

	public void setListString(String listString) {
		this.listString = listString;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getFlow() {
		return flow;
	}

	public void setFlow(String flow) {
		this.flow = flow;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getEncode() {
		return Encode;
	}

	public void setEncode(String encode) {
		Encode = encode;
	}

	public CodeInvoice getCodeInvoice() {
		return codeInvoice;
	}

	public void setCodeInvoice(CodeInvoice codeInvoice) {
		this.codeInvoice = codeInvoice;
	}

	public CodeInvoiceService getCodeInvoiceService() {
		return codeInvoiceService;
	}

	public void setCodeInvoiceService(CodeInvoiceService codeInvoiceService) {
		this.codeInvoiceService = codeInvoiceService;
	}

	public Code getCode() {
		return code;
	}

	public void setCode(Code code) {
		this.code = code;
	}

	public CodeService getCodeService() {
		return codeService;
	}

	public void setCodeService(CodeService codeService) {
		this.codeService = codeService;
	}

	public Collection<CodeInvoice> getCodelist() {
		return codelist;
	}

	public void setCodelist(Collection<CodeInvoice> codelist) {
		this.codelist = codelist;
	}

	public String getUploadS() {
		return uploadS;
	}

	public void setUploadS(String uploadS) {
		this.uploadS = uploadS;
	}

}
